package com.sakura.mall.coupon.memberPrice.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.memberPrice.domain.entity.MemberPriceEntity;
import com.sakura.mall.coupon.memberPrice.domain.request.AddMemberPriceRequest;
import com.sakura.mall.coupon.memberPrice.domain.request.UpdateMemberPriceRequest;
import com.sakura.mall.coupon.memberPrice.domain.request.MemberPricePageRequest;
import com.sakura.mall.coupon.memberPrice.domain.vo.MemberPriceVo;
import com.sakura.mall.coupon.memberPrice.mapper.MemberPriceMapper;
import com.sakura.mall.coupon.memberPrice.service.MemberPriceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品会员价格 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberPriceServiceImpl extends ServiceImpl<MemberPriceMapper, MemberPriceEntity> implements MemberPriceService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberPriceRequest request) {
        // todo 其他校验逻辑
        MemberPriceEntity memberPrice = request.convert(MemberPriceEntity.class);
        this.save(memberPrice);
    }

    @Override
    public Page<MemberPriceVo> queryPage(MemberPricePageRequest request) {
        IPage<MemberPriceEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberPriceEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberPriceVo> ret = qp.convert(entity -> entity.convert(MemberPriceVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberPriceRequest request) {
        MemberPriceEntity entity = request.convert(MemberPriceEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

